<template>
  <el-dialog
    :title="!tableDataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      size="small"
      :model="tableDataForm"
      :rules="dataRule"
      ref="tableDataForm"
      @keyup.enter.native="tableDataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="表名称" prop="name">
        <el-input v-model="tableDataForm.name" placeholder="表名称"></el-input>
      </el-form-item>

      <el-form-item
        label="表物理名称"
        v-if="physicalNameShow"
        prop="physicalName"
      >
        <el-input
         :disabled="physicalNameDisabled"
          v-model="tableDataForm.physicalName"
          placeholder="表物理名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="表编码" prop="code">
        <el-input v-model="tableDataForm.code" placeholder="表编码"></el-input>
      </el-form-item>

      <el-form-item label="表类型" prop="tableType">
        <el-radio-group v-model="tableDataForm.tableType">
          <el-radio :label="1">主表</el-radio>
          <el-radio :label="2">从表</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="提示信息" prop="tooltip">
        <el-input
          v-model="tableDataForm.tooltip"
          placeholder="提示信息"
        ></el-input>
      </el-form-item>

      <el-form-item label="表备注" prop="remark">
        <el-input v-model="tableDataForm.remark" placeholder="备注"></el-input>
      </el-form-item>

      <el-form-item label="排序号" prop="orderNum">
        <el-input-number
          v-model="tableDataForm.orderNum"
          controls-position="right"
          :min="0"
          label="排序号"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="表状态" size="mini">
        <el-radio-group v-model="tableDataForm.dataState">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="tableDataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>


<script>
import { saveOrUpdateTable } from "@/api/table";
export default {
  data() {
    return {
      visible: false,
      physicalNameShow: false,
      physicalNameDisabled: false,
      tableDataForm: {
        id: "",
        code: "",
        parentId: "",
        physicalName: "",
        tooltip: "",
        tableType: "",
        name: "",
        remark: "",
        orderNum: 0,
        dataState: 0,
      },
      dataRule: {
        name: [{ required: true, message: "表名称不能为空", trigger: "blur" }],
        code: [{ required: true, message: "表编码不能为空", trigger: "blur" }],
        tableType: [
          { required: true, message: "表类型不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {},
  activated() {},
  methods: {
    init(data) {
      this.tableDataForm.id = 0;

      if (data.addChildren == 1) {
        // 添加子节点
        this.tableDataForm.parentId = data.id;
        this.physicalNameShow = false;
        this.physicalNameDisabled = false;
      } else if (data.addChildren == 2) {
        // 编辑
        this.physicalNameShow = true;
        this.physicalNameDisabled = true;
        // 修改
        this.tableDataForm.id = data.id;
        this.tableDataForm.name = data.name;
        this.tableDataForm.parentId = data.parentId;
        this.tableDataForm.url = data.url;
        this.tableDataForm.physicalName = data.physicalName;

        this.tableDataForm.tooltip = data.tooltip;
        this.tableDataForm.tableType = data.tableType;

        this.tableDataForm.orderNum = data.orderNum;
        this.tableDataForm.remark = data.remark;
        this.tableDataForm.dataState = data.dataState;
        this.tableDataForm.code = data.code;
      }

      this.visible = true;
      this.$nextTick(() => {
        this.$refs["tableDataForm"].resetFields();
      });
    },
    tableDataFormSubmit() {
      this.$refs["tableDataForm"].validate((valid) => {
        if (valid) {
          saveOrUpdateTable({
            id: this.tableDataForm.id || "",
            parentId: this.tableDataForm.parentId || 0,
            name: this.tableDataForm.name,
            code: this.tableDataForm.code,
            remark: this.tableDataForm.remark,
            tooltip: this.tableDataForm.tooltip,
            tableType: this.tableDataForm.tableType,
            orderNum: this.tableDataForm.orderNum,
            dataState: this.tableDataForm.dataState,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
